<?php

namespace app\common\library;

/**
 * 搜索历史记录工具类
 */
class SearchLog
{
    /**
     * 记录搜索历史
     * @param int $user_id 用户ID
     * @param string $keyword 搜索关键词
     * @param string $type 搜索类型
     * @return bool
     */
    public static function record($user_id, $keyword, $type)
    {
        if(!$user_id || !$keyword || !$type) {
            return false;
        }
        $keyword = trim($keyword);
        if(empty($keyword)){
            return false;
        }
        $searchHistory = \app\common\model\SearchHistory::where([
            'user_id' => $user_id,
            'keyword' => $keyword,
            'type' => $type
        ])->find();
        
        if($searchHistory) {
            return $searchHistory->save(['createtime' => time()]);
        } else {
            return \app\common\model\SearchHistory::create([
                'user_id' => $user_id,
                'keyword' => $keyword,
                'type' => $type,
                'createtime' => time()
            ]);
        }
    }
}